package com.example.victory.service;

import com.example.victory.entity.companyEntity.AccountIncompleteEntity;
import com.example.victory.mapper.CustomerMapper;
import com.example.victory.swagger.handler.MiniAppAccountIncompleteApiDelegate;
import com.example.victory.swagger.model.*;
import com.example.victory.utils.AnalysisUtils;
import com.example.victory.utils.CrmUtils;
import com.cgws.fintech.sdk.api.inner.security.model.User;
import com.cgws.fintech.sdk.api.mvp.model.ApiRequestCustomerQueryData;
import com.cgws.fintech.sdk.api.mvp.model.CustomerAccount;
import com.cgws.fintech.sdk.common.utils.MoneyHelper;
import com.example.victory.common.web.ServiceHelper;
import com.cgws.fintech.sdk.inner.utils.web.SessionService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;


@Service
public class MiniappAccountIncomplete implements MiniAppAccountIncompleteApiDelegate{

    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private MvpLoginService mvpLoginService;
    @Autowired
    private SessionService sessionService;

    @Override
    public ResponseEntity<ApiResponseAccountIncomplete> getAccountIncomplete(ApiRequestAccountIncomplete param) {
        ApiRequestAccountIncompleteData data = param.getData();
        ServiceHelper.checkDataNotNull(data, "data");
        ApiRequestAccountIncompleteData.ExceptionTypeEnum exceptionType = data.getExceptionType();
        ServiceHelper.checkDataNotNull(exceptionType, "data.exceptionType");
        Integer index = data.getIndex();
        Integer pageSize = data.getPageSize();
        if (index == null) index = 0; // 首页
        if (pageSize == null) pageSize = 20; // 默认20条

        // 获取当前用户
        User user = sessionService.getCurrentUserSession(true).getUser();
        String crmId = user.getCrmId();
        if (crmId == null) crmId = "";
//        crmId = "1000581";

        ApiResponseAccountIncompleteData incompleteData = new ApiResponseAccountIncompleteData();

        List<AccountIncompleteEntity> entityList = new ArrayList<>();
        List<AccountIncomplete> custInfoList = new ArrayList<>();
        List<ApiRequestCustomerQueryData> customerQueryDataList = Lists.newArrayList();
        if (Objects.equals(exceptionType, ApiRequestAccountIncompleteData.ExceptionTypeEnum.THREE_WAY_PIPE)) {// 三方存管异常
            entityList = customerMapper.getThreeWayPipe(crmId, index * pageSize, (index + 1) * pageSize);

            if (entityList == null || entityList.size() == 0) {
                incompleteData.setAccountIncomplete(Collections.emptyList());
                return ResponseEntity.ok(new ApiResponseAccountIncomplete().data(incompleteData));
            }

            entityList.forEach(entity -> {
                ApiRequestCustomerQueryData customerQueryData=new ApiRequestCustomerQueryData();
                customerQueryData.setCustNum(entity.getCustomerId());
                customerQueryDataList.add(customerQueryData);

                AccountIncomplete incomplete = new AccountIncomplete();
                incomplete.setName(entity.getName());
                incomplete.setCustomerId(entity.getCustomerId());
                incomplete.setRelationType(AnalysisUtils.getRelationType(entity.getRelationType()));
                if (entity.getRiskLevel() != null) incomplete.setRiskLevel(entity.getRiskLevel());
                if (entity.getCustLevel() != null) incomplete.setCustLevel(entity.getCustLevel());
                incomplete.setPhone(entity.getPhone());

                ArrayList<KeyValueDetail> keyValueList = Lists.newArrayList();
                KeyValueDetail threeWayPipe = new KeyValueDetail();
                threeWayPipe.setName("三方存管");
                threeWayPipe.setValue(entity.getThreeWayPipe());
                keyValueList.add(threeWayPipe);
                KeyValueDetail openDate = new KeyValueDetail();
                openDate.setName("开户时间");
                openDate.setValue(entity.getOpenDate());
                keyValueList.add(openDate);
                KeyValueDetail shenASecuid = new KeyValueDetail();
                shenASecuid.setName("深A股东卡");
                shenASecuid.setValue(entity.getShenASecuid());
                keyValueList.add(shenASecuid);
                KeyValueDetail huASecuid = new KeyValueDetail();
                huASecuid.setName("沪A股东卡");
                huASecuid.setValue(entity.getHuASecuid());
                keyValueList.add(huASecuid);

                incomplete.setDetails(keyValueList);
                custInfoList.add(incomplete);


            });
        } else {// 股东卡异常
            entityList = customerMapper.getShareholdersCard(crmId, index * pageSize, (index + 1) * pageSize);

            if (entityList == null || entityList.size() == 0) {
                incompleteData.setAccountIncomplete(Collections.emptyList());
                return ResponseEntity.ok(new ApiResponseAccountIncomplete().data(incompleteData));
            }

            entityList.forEach(entity -> {
                ApiRequestCustomerQueryData customerQueryData=new ApiRequestCustomerQueryData();
                customerQueryData.setCustNum(entity.getCustomerId());
                customerQueryDataList.add(customerQueryData);

                AccountIncomplete incomplete = new AccountIncomplete();
                incomplete.setName(entity.getName());
                incomplete.setCustomerId(entity.getCustomerId());
                incomplete.setRelationType(AnalysisUtils.getRelationType(entity.getRelationType()));
                if (entity.getRiskLevel() != null) incomplete.setRiskLevel(entity.getRiskLevel());
                if (entity.getCustLevel() != null) incomplete.setCustLevel(entity.getCustLevel());
                incomplete.setPhone(entity.getPhone());

                ArrayList<KeyValueDetail> keyValueList = Lists.newArrayList();
                KeyValueDetail shenASecuid = new KeyValueDetail();
                shenASecuid.setName("深A股东卡");
                shenASecuid.setValue(entity.getShenASecuid());
                keyValueList.add(shenASecuid);
                KeyValueDetail huASecuid = new KeyValueDetail();
                huASecuid.setName("沪A股东卡");
                huASecuid.setValue(entity.getHuASecuid());
                keyValueList.add(huASecuid);

                incomplete.setDetails(keyValueList);
                custInfoList.add(incomplete);
            });
        }
        if(customerQueryDataList.size()>0){
            List<CustomerAccount> customerAccountList = CrmUtils.getCustAccountListByIds(mvpLoginService, customerQueryDataList);
            if (customerAccountList != null) {
                custInfoList.forEach(customer -> {
                    customerAccountList.forEach(customerAccount->{
                        if(!StringUtils.isEmpty(customerAccount.getFcid())&&customer.getCustomerId().equals(customerAccount.getCustNum())){
                            customer.setImId(customerAccount.getFcid());
                        }
                    });
                });
            }
        }

        return ResponseEntity.ok(new ApiResponseAccountIncomplete().data(incompleteData.accountIncomplete(custInfoList)));
    }
}
